﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;

namespace VkSample82
{
    public class InputSystem : ComponentSystem
    {
        private Ambient Ambient = new Ambient(0.1f, 0.1f, 0.1f, 0.1f);
        private float LightFWJ;
        private Camera Camera;

        public InputSystem()
        {
        }

        public override void Initialization()
        {
            ContextManager.Foreach<LightGroup>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                var obj = context.GetContextRenderObject();
                var render = context.GetContextRenderGroup();
                var camera = render.Camera;
                Camera = camera;
                ParallelLight light = obj.GetComponents<ParallelLight>().First();
                Span<Vector4> data = stackalloc Vector4[]
                {
                    new Vector4(camera.EyePosition, 1),
                    light.Direction,
                    Ambient,
                    light.Strength,
                    light.Strength
                };
                Span<byte> p = MemoryMarshal.AsBytes(data);
                render.PushDataToTexture(p, 0, 0);
            });
        }

        public override void Update(TimeSpan lastStamp)
        {
            if (Input.GetMouseButtonState(MouseButton.ButtonLeft).Action == InputAction.Press)
            {
                Vector2 p = Input.GetMousePositionDelta();
                if (p.Y != 0)
                {
                    Camera.EncircleForX(Vector3.Zero, -p.Y * 0.5f * MathF.PI / 180.0f);
                }
                ContextManager.Foreach<LightGroup>((group) =>
                {
                    Context context = ContextManager.GetContext(group.Entity);
                    var obj = context.GetContextRenderObject();
                    var render = context.GetContextRenderGroup();
                    ParallelLight light = obj.GetComponents<ParallelLight>().First();
                    var camera = render.Camera;
                    if (p.X != 0)
                    {
                        LightFWJ += p.X * 180 / 1000;
                        light.ChangeDirection(new Vector3(MathF.Sin(LightFWJ / 180 * MathF.PI) * 300, 0, MathF.Cos(LightFWJ / 180 * MathF.PI) * 300));
                    }
                    Span<Vector4> data = stackalloc Vector4[]
                    {
                        new Vector4(camera.EyePosition, 1),
                        light.Direction,
                        Ambient,
                        light.Strength,
                        light.Strength
                    };
                    Span<byte> d = MemoryMarshal.AsBytes(data);
                    render.PushDataToTexture(d, 0, 0);
                });
            }
        }
    }

    public struct LightGroup
    {
        public Entity Entity;
        public LightComponent Light;
    }
}
